#!/bin/bash
#
# xLiveServerd        Startup script for the xLiveServer
#
# chkconfig: - 85 15
# description: The xLiveServer is an efficient and extensible  \
#	       server implementing the current HTTP standards.
# processname: xLiveServerd


# Source function library.
. /etc/rc.d/init.d/functions


# Path to the xLiveServerd script, server binary, and short-form for messages.
RETVAL=0
xLiveServerd=/usr/local/sbin/xLiveServer/xLiveServer
prog=xLiveServerd
lockfile=/var/lock/subsys/$prog
pidfilepath=/var/run/xServer
logfilepath=/var/log/xLiveServer
STOP_TIMEOUT=${STOP_TIMEOUT-10}

#create pid file path,
#because proc will create a pid file when start server
if [ -d $pidfilepath ];then   
	echo "pid file path:$pidfilepath"  
else 
	mkdir $pidfilepath
	chmod 777 $pidfilepath
fi

#create log  file path,
#because proc will create log file when server run
if [ -d $logfilepath ];then   
	echo "log file path:$logfilepath"  
else 
	mkdir $logfilepath
	chmod 777 $logfilepath
fi

# set server start file des max numbers
# server must start confit at max conenction 65536 / 2 = 32768 ,and max stack memory 10240 KB=10MB
ulimit  -n  65536
ulimit  -s  10240

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        daemon $xLiveServerd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping xLiveServerd, a delay (of default 10 second) is required
# before SIGKILLing the xLiveServerd parent; this gives enough time for the
# xLiveServerd parent to SIGKILL any errant children.
stop() {
	echo -n $"Stopping $prog: "
	killproc  -d ${STOP_TIMEOUT} $xLiveServerd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${lockfile}
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;; 
  status)
    status $xLiveServerd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;; 
  reload)
	exit 3
	;;
  *)
	echo $"Usage: $prog {start|stop|status|restart}"
	RETVAL=2
esac

exit $RETVAL